import time
from multiprocessing import Process,Queue

"""定义装饰器"""
def pro_zsq(fun):
    def inner(*args,**kwargs):
        print("produce start")
        btime = time.time()
        fun(*args,**kwargs)
        etime = time.time()
        use_time = etime - btime
    return inner

class Produce(Process):
    def __init__(self,queue):
        Process.__init__(self)
        self.__queue = queue
    @pro_zsq
    def run(self):
        """
        逻辑处理
        :return:
        """
        print("produce start")
        btime = time.time()
        for i in range(100):
            self.__queue.put(i)
            print(f"produce put data :{i}")
        etime = time.time()
        use_time = etime - btime
        print(f"produce end,总共耗时:{use_time}s")


class Consumer(Process):
    def __init__(self, queue):
        Process.__init__(self)
        self.__queue = queue

    def run(self):
        """
        逻辑处理
        :return:
        """
        print("consumer start")
        btime = time.time()
        while True:
            try:
                item = self.__queue.get_nowait()
                print(f"consumer get data :{item}")
            except Exception as e:
                print("consumer has error")
                break
        etime = time.time()
        use_time = etime - btime
        print(f"consumer end,总共耗时:{use_time}s")

if __name__ == "__main__":
    queue = Queue()
    p_pro = Produce(queue)
    p_con = Consumer(queue)

    p_pro.start()
    p_con.start()

    p_pro.join()
    p_con.join()
